package com.hp.easyBuy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hp.easyBuy.dao.UserDao;
import com.hp.easyBuy.entity.TUser;
import com.hp.easyBuy.service.UserService;
import com.hp.easyBuy.utils.PasswordUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, TUser> implements UserService {
    @Resource
    private UserDao userDao;


    @Override
    public TUser getUserByUsercode(String userCode) {
        QueryWrapper<TUser> queryWrapper = new QueryWrapper<TUser>();
        queryWrapper.eq("USERCODE", userCode);
        TUser tUser = userDao.selectOne(queryWrapper);
        return tUser;
    }

    @Override
    public void register(TUser user) {
        //随机获取一个盐值
        String salt = PasswordUtil.salt();
        //对用户的密码进行加密和加盐处理
        String encodePwd = PasswordUtil.encode(user.getPassword(), salt);
        user.setSalt(salt);
        user.setPassword(encodePwd);
        //设置状态为启用，职位为普通用户
        user.setType(2);
        user.setStatus(1);
        userDao.insert(user);
    }

    @Override
    public TUser login(String usercode, String password) {
        //根据用户名获取用户信息
        TUser user = getUserByUsercode(usercode);
        System.out.println("user = " + user);
        if(user == null){
            //该用户名不存在，返回失败信息
            return null;
        }
        //该用户名存在，验证密码是否正确
        String salt = user.getSalt();
        String encodePwd = PasswordUtil.encode(password, salt);
        if(encodePwd.equals(user.getPassword())){
            //如果用户传来的密码加密加盐之后与数据库的密码相同，证明密码正确
            return user;
        }else {
            //密码不同，证明密码错误，返回错误
            return null;
        }
    }

    //根据用户名获取用户，将输入的原密码与数据库的密码比对
    @Override
    public boolean checkUser(String userId, String passWord) {
        boolean result=false;
        TUser user = this.getUserByUserid(userId);
        System.out.println(user);
        String encode = PasswordUtil.encode(passWord, user.getSalt());
        if(encode.equals(user.getPassword())){
            result=true;
        }
        return result;
    }

    //修改用户密码返回布尔
    @Override
    public boolean updatePwd(String userId, String passWord) {
        TUser user = this.getUserByUserid(userId);
        String salt = PasswordUtil.salt();
        String pwd=PasswordUtil.encode(passWord,salt);
        user.setSalt(salt);
        user.setPassword(pwd);
        UpdateWrapper<TUser> updateWrapper = new UpdateWrapper<TUser>();
        updateWrapper.eq("USERID",userId);
        return userDao.update(user,updateWrapper)>0;
    }

    //根据用户ID获取用户信息
    @Override
    public TUser getUserByUserid(String userId) {
        QueryWrapper<TUser> queryWrapper = new QueryWrapper<TUser>();
        queryWrapper.eq("USERID", userId);
        TUser tUser = userDao.selectOne(queryWrapper);
        return tUser;
    }


}
